<template>
  <div class="page">
    <iframe :src="newUrl(iframeUrl[currentUrl].url)"></iframe>
  </div>
</template>
<script>
  export default {
    name: 'applications',
    data () {
      return {
        token: {},
        currentUrl: this.$route.fullPath.split('?')[1]
      }
    },
    computed: {
      iframeUrl () {
        return this.$store.state.thirdApp
      },
      ur () {
        return this.$store.state.session.user
      }
    },
    watch: {
      $route (to, from) {
        const routeArr = to.path.split('/')
        if (routeArr[2] === 'applications') {
          document.title = this.iframeUrl[this.$route.fullPath.split('?')[1]].content + ' - 云客服'
          this.currentUrl = this.$route.fullPath.split('?')[1]
        }
      }
    },
    methods: {
      newUrl (url) {
        let newUrl = url.indexOf('?') !== -1 ? url + '&loginName=' + this.ur.displayName + '&loginExten=' + this.ur.exten : url + '?loginName=' + this.ur.displayName + '&loginExten=' + this.ur.exten
        newUrl += '&accountId=' + this.$store.state.session.account.account
        if (newUrl.indexOf('power.7moor.com') > -1 || newUrl.indexOf('power-') > -1 || newUrl.indexOf('127') > -1) {
          if (this.$store.state.session.sessionId && window.sessionStorage.getItem('loginToken')) {
            newUrl += '&sessionId=' + this.$store.state.session.sessionId + '&loginToken=' + window.sessionStorage.getItem('loginToken')
          }
        }
        if (this.$store.state.session.account.tokenCheck) {
          newUrl += '&token=' + this.token.token + '&tokenId=' + this.token.tokenId
        }
        return newUrl
      }
    },
    beforeMount () {
      let url = this.$route.fullPath.split('?')[1]
      document.title = this.iframeUrl[url].content + ' - 云客服'
      if (this.$store.state.session.account.tokenCheck) {
        this.$store.dispatch('getToken').then((token) => {
          this.token = token
        })
      }
    }
  }
</script>
<style lang="stylus" scoped>
  .page
    height calc(100vh - 54px)
    height -webkit-calc(100vh - 54px)
    height -moz-calc(100vh - 54px)
    overflow hidden
    position relative
    background $cf-white
    iframe
      position absolute
      width 100%
      height 100%
      border none
      overflow auto
      display block
</style>